/*
 *  Copyright (C) 2011 Dan de la Cruz <ddcruzg@gmail.com>
 * 
 *  Viajero Fashion is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  Viajero Fashion is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.vfashion.dao.impl.mysql;

import com.vfashion.dao.PaqueteDAO;
import com.vfashion.dao.factory.DAOFactory;
import com.vfashion.domain.Paquete;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Dan de la Cruz <ddcruzg@gmail.com>
 */

public class MysqlPaqueteDAO implements PaqueteDAO{

    private Connection mysql;

    public MysqlPaqueteDAO() {
        mysql = DAOFactory.getInst().getConexion();
    }

    @Override
    public Paquete getById(int idpaquete) throws Exception {
         Paquete paquete = new Paquete();
        try {
            mysql.setAutoCommit(false);
            String consulta = "SELECT idpaquete,dias,noches,imagen,"
                    + "atractivo,lugar,precio,descripcion FROM paquete "
                    + "WHERE idpaquete = ?";
            PreparedStatement pstmt = mysql.prepareStatement(consulta);
            pstmt.setInt(1, idpaquete);

            ResultSet rs = pstmt.executeQuery();

            while (rs.next()) {
                paquete.setIdpaquete(rs.getInt(1));
                paquete.setDias(rs.getString(2));
                paquete.setNoches(rs.getString(3));
                paquete.setImagen(rs.getString(4));
                paquete.setAtractivo(rs.getString(5));
                paquete.setLugar(rs.getString(6));
                paquete.setPrecio(rs.getDouble(7));
                paquete.setDescripcion(rs.getString(8));
            }
            mysql.setAutoCommit(true);
        } catch (Exception e) {
            mysql.rollback();
            System.out.println(e.getMessage());
            throw new Exception("DB : error  entregando categoria");
        } finally {
            try {
                System.out.println("entrega de paquete getById satisfactorio");
                mysql.close();
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }
        return paquete;
    }

    @Override
    public List<Paquete> getAll() throws Exception {
        Paquete paquete = null;
        List<Paquete> lstPaquetes = null;
        try {
            mysql.setAutoCommit(false);
            String consulta = "SELECT idpaquete,dias,noches,imagen,"
                    + "atractivo,lugar,precio,descripcion FROM paquete ";
            PreparedStatement pstmt = mysql.prepareStatement(consulta);
            ResultSet rs = pstmt.executeQuery();
            if (rs != null) {
                lstPaquetes = new ArrayList<Paquete>();
            }
            while (rs.next()) {
                paquete = new Paquete();
                paquete.setIdpaquete(rs.getInt(1));
                paquete.setDias(rs.getString(2));
                paquete.setNoches(rs.getString(3));
                paquete.setImagen(rs.getString(4));
                paquete.setAtractivo(rs.getString(5));
                paquete.setLugar(rs.getString(6));
                paquete.setPrecio(rs.getDouble(7));
                paquete.setDescripcion(rs.getString(8));
                lstPaquetes.add(paquete);
            }
            mysql.setAutoCommit(true);
        } catch (Exception e) {
            mysql.rollback();
            System.out.println(e.getMessage());
        } finally {
            try {
                System.out.println("entrega de paquetes getById satisfactorio");
                mysql.close();
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }
        return lstPaquetes;
    }

}
